package com.springcloudemo.ms;

import org.apache.dubbo.config.ApplicationConfig;
import org.apache.dubbo.config.ProtocolConfig;
import org.apache.dubbo.config.ReferenceConfig;
import org.apache.dubbo.config.RegistryConfig;
import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.apache.dubbo.rpc.service.GenericService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;


/**
 * Hello world!
 *
 */
@SpringBootApplication
@EnableDiscoveryClient
@EnableDubbo
public class DubboBroadcastApplication {

    public static void main(String[] args) {
        SpringApplication.run(DubboBroadcastApplication.class, args);
    }

    @RestController
    class EchoController {

        @Value("${server.port}")
        private String port;



        @RequestMapping(value = "/echo/{string}", method = RequestMethod.GET)
        public String echo(@PathVariable String string) {

            ApplicationConfig applicationConfig = new ApplicationConfig();
            applicationConfig.setName("dubbo-discovery-generic");
            ProtocolConfig protocolConfig = new ProtocolConfig();
            protocolConfig.setPort(29080);
            protocolConfig.setName("dubbo");

            RegistryConfig registryConfig = new RegistryConfig();
            registryConfig.setAddress("nacos://127.0.0.1:8848");

            ReferenceConfig<GenericService> referenceConfig = new ReferenceConfig<>();
            referenceConfig.setInterface("com.springcloudemo.ms.facede.DemoService");
            referenceConfig.setApplication(applicationConfig);
            referenceConfig.setProtocol("dubbo");
            referenceConfig.setRegistry(registryConfig);
            referenceConfig.setGeneric("true");
            referenceConfig.setTimeout(5000);

            GenericService genericService = referenceConfig.get();
            Object sayHello = genericService.$invoke("sayHello",
                    new String[]{String.class.getName()},
                    new Object[]{"Geek"});

            return  port + " "+sayHello.toString();
        }
    }
}